#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &x)
{
    char c;
    int f = 1;
    x = 0;
    for (c = getchar(); !isdigit(c); c = getchar())
        if (c == '-')
            f = -f;
    for (; isdigit(c); c = getchar())
        x = x * 10 + (c ^ '0');
    x *= f;
}
template <typename T>
inline void chkmx(T &x, T y) { x = max(x, y); }
typedef long long ll;
int n;
int a[200020];
int col[200020];
vector<int> vec[200020];
void solve()
{
    read(n);
    for (int i = 1; i <= n; i++)
        read(a[i]);
    ll ans = 0;
    map<int, ll> v1l, v1r;
    map<int, ll> v2l, v2r;
    v1l[0] = 0;
    v1l[0] = 0;
    ll addL = 0, addR = 0;
    ll mxL = 0, mxR = 0;
    for (int i = 1; i <= n; i++)
    {
        v2l = v1l;
        v2r = v1r;
        ll aL = 0, aR = 0;
        ll mL = mxL, mR = mxR;
        if (a[i - 1] == a[i])
        {
            if (v1l.count(a[i]))
                v2r[a[i - 1]] = v1l[a[i]] + a[i];
            chkmx(v2r[a[i - 1]], mxL);
            chkmx(mR, v2r[a[i - 1]]);
            for (auto [r, w] : v1l)
                chkmx(v2l[r], w + a[i]);
            // aL += a[i];
        }
        else
        {
            if (v1l.count(a[i]))
                v2r[a[i - 1]] = v1l[a[i]] + a[i];
            chkmx(v2r[a[i - 1]], mxL);
            chkmx(mR, v2r[a[i - 1]]);
        }
        if (a[i - 1] == a[i])
        {
            if (v1r.count(a[i]))
                v2l[a[i - 1]] = v1r[a[i]] + a[i];
            chkmx(v2l[a[i - 1]], mxR);
            chkmx(mL, v2l[a[i - 1]]);
            for (auto [l, w] : v1r)
                chkmx(v2r[l], w + a[i]);
            // aR += a[i];
        }
        else
        {
            if (v1r.count(a[i]))
                v2l[a[i - 1]] = v1r[a[i]] + a[i];
            chkmx(v2l[a[i - 1]], mxR);
            chkmx(mL, v2l[a[i - 1]]);
        }
        v1l = v2l;
        v1r = v2r;
        // mxL = mL + aL, mxR = mR + aR;
        // addL = aL, addR = aR;
        for (auto [t, w] : v1l)
            chkmx(mxL, w);
        for (auto [t, w] : v1r)
            chkmx(mxR, w);
    }
    cout << max(mxL, mxR) << '\n';
}
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int t;
    read(t);
    while (t--)
        solve();
    return 0;
}